package com.ohuang.jiaxiaotong.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.base.BaseAc;
import com.ohuang.jiaxiaotong.entity.User;
import com.ohuang.jiaxiaotong.fragment.CourseTable2Fragment;
import com.ohuang.jiaxiaotong.fragment.CourseTableFragment;
import com.ohuang.jiaxiaotong.fragment.DataFile2Fragment;
import com.ohuang.jiaxiaotong.fragment.DataFileFragment;
import com.ohuang.jiaxiaotong.fragment.Notice2Fragment;
import com.ohuang.jiaxiaotong.fragment.NoticeFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


public class MainClassActivity extends BaseAc {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> listFragment;
    private List<String> listTitle;
    private FragmentPagerAdapter mAdapter;
    private String className;
    private User user;
    private TextView textView_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_class);
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        className = intent.getStringExtra("className");
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        Ohuang.changeStatusBarTextImgColor(this, true);//手机状态栏设置黑色
        textView_title = findViewById(R.id.txt_title_mainClass);
        textView_title.setText("班级："+className);

        tabLayout = findViewById(R.id.tab_layout_mainClass);
        viewPager = findViewById(R.id.viewPage_mainClass);
        if (user != null) {
            if ("0".equals(user.getRole())) {
                initView();
            } else {
                initView2();
            }

        }
    }

    private void initView() {
        listFragment = new ArrayList<>(3);
        listTitle = new ArrayList<>(3);
        listTitle.add("课表");
        listFragment.add(new CourseTableFragment(className));
        listTitle.add("公告");
        listFragment.add(new NoticeFragment(className));
        listTitle.add("资料");
        listFragment.add(new DataFileFragment(className));

        mAdapter = new FragmentAdapter(getSupportFragmentManager(), listFragment, listTitle);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(2);//设置viewPage缓存左右各2个页面
        tabLayout.setupWithViewPager(viewPager);//与ViewPage建立关系
    }

    private void initView2() {
        listFragment = new ArrayList<>(3);
        listTitle = new ArrayList<>(3);
        listTitle.add("课表");
        listFragment.add(new CourseTable2Fragment(className));
        listTitle.add("公告");
        listFragment.add(new Notice2Fragment(className));
        listTitle.add("资料");
        listFragment.add(new DataFile2Fragment(className));
        mAdapter = new FragmentAdapter(getSupportFragmentManager(), listFragment, listTitle);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(2);//设置viewPage缓存左右各2个页面
        tabLayout.setupWithViewPager(viewPager);//与ViewPage建立关系
    }

    public void MainClassBackClick(View view) {
        finish();
    }

    public void startClassInfoClick(View view) {
        Intent intent=new Intent(this,ClassInfoActivity.class);
        intent.putExtra("className",className);
        startActivity(intent);
    }


    private class FragmentAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragmentList;//各导航的Fragment
        private List<String> mTitle; //导航的标题

        public FragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> title) {
            super(fragmentManager);
            mFragmentList = fragments;
            mTitle = title;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle.get(position);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void GetUserEventBus(User user) {
        this.user = user;
    }

}
